import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'
export const communityStore = defineStore('community', () => { 
    const index_Carousel_allinfo=ref([])
    const user_search_history = ref([])
    
    // 这个文本是社区获取的，不是自己的
    const text_data = reactive({
        sift: [],
        tech: [],
        life: [],
        game: [],
        travel: [],
        food: [],
        hot:[]
    })
    const search_text_data = ref({})
    const my_text_info = reactive({}) //这个实际上只存储了id等基本信息,实际获取具体信息用额外的api
    const my_text_info_all = ref({})  //这个是全信息通过id键存储
    const searchQuery = ref('')
    let curent_article_expand_info = ref({})
    let current_reader_article = ref(null)
    const mycomments = reactive({})  //由于评论信息较少直接全部存储
    const set_current_reader_article = function (data) {
        current_reader_article.value = data
    }
    const get_current_reader_article = function () {
        return current_reader_article.value
    }
    const set_index_Carousel_info_All = function (data) {
       index_Carousel_allinfo.value=data
    }
    const get_index_Carousel_info_ALL = function (){
        return index_Carousel_allinfo.value
    }
    const get_index_Carousel_info_Bytitle = function (title) { 
        return index_Carousel_allinfo.value.find((item) => {
               return item.title === title
        })
    }
    const get_text_data = (kind) => { 
        // console.log(text_data[kind])
        return text_data[kind]
    }
    const set_text_data = (kind,data) => {
        text_data[kind] = data
    }
    const set_curent_article_expand_info= function (data,level) {
        curent_article_expand_info.value[level]=data
    }
    const get_curent_article_expand_info = function (level) {
        return curent_article_expand_info.value[level]
    }
    const get_search_text_data = function () {
        return search_text_data.value
    }
    const set_search_text_data = function (data) {
        search_text_data.value = data
    }
    const get_searchQuery= function () {
        return searchQuery.value
    }
    const set_searchQuery = function (data) {
        searchQuery.value = data
    }
    const set_my_text_info = (selection, value) => {
        my_text_info[selection] = my_text_info[selection] || {}
        my_text_info[selection].data = value
    }
      
    const get_my_text_info = (selection) => {
        return (my_text_info[selection]?.data) || []
    }      
    const pop_my_text_info = (id) => {
        // 于多个类别可能会有重复的数据
        Object.keys(my_text_info).forEach((key) => {
            my_text_info[key].data= my_text_info[key].data.filter((i)=>i.id!=id)
        })
    }
    const set_my_text_info_meta = (selection, meta) => {
        my_text_info[selection] = my_text_info[selection] || {}
        my_text_info[selection].meta = meta
    }

    const get_my_text_info_meta = (selection) => {
        return my_text_info[selection]?.meta || {}
    }
    const set_my_text_info_all=(id,value)=>{
        my_text_info_all.value[id]=value
    }
    const get_my_text_info_all=(id)=>{
        return my_text_info_all.value[id] 
    }
    const set_my_comments_data = (selection, value) => { 
        mycomments[selection]= mycomments[selection] || {}
        mycomments[selection].data=value
    }
    const pop_my_comments_data_by_id = (id) => { 
        Object.keys(mycomments).forEach((key) => {
            mycomments[key].data=mycomments[key].data.filter((i)=>i.id!=id)
        })
    }
    const get_my_comments_data_by_id = (selection,id) => { 
        return mycomments[selection].data.filter((i)=>i.id==id)[0]
    }
    const get_my_comments_data_by_selection = (selection) => { 
        return mycomments[selection]?.data || []
    }
    const get_my_comments_meta = (selection) => { 
        return mycomments[selection]?.meta || {}
    }
    const set_my_comments_meta = (selection, value) => {
        mycomments[selection] = mycomments[selection] || {}
        mycomments[selection].meta=value
    }


    return {
        index_Carousel_allinfo,search_text_data,
        searchQuery,
        user_search_history,
        current_reader_article,
        curent_article_expand_info,my_text_info,my_text_info_all,mycomments,
        get_index_Carousel_info_ALL,set_index_Carousel_info_All, get_index_Carousel_info_Bytitle,
        get_text_data,set_text_data,
        set_current_reader_article, get_current_reader_article,
        set_curent_article_expand_info, get_curent_article_expand_info,
        get_search_text_data, set_search_text_data,
        get_searchQuery, set_searchQuery,
        set_my_text_info, get_my_text_info, pop_my_text_info, set_my_text_info_meta, get_my_text_info_meta,
        set_my_text_info_all, get_my_text_info_all,
        set_my_comments_data, pop_my_comments_data_by_id,
        get_my_comments_data_by_id, get_my_comments_data_by_selection,get_my_comments_meta,set_my_comments_meta
    }
},
{
       persist: {
                enabled: true,
                strategies: [
                        {
                                key: 'user_search_history',
                                storage: sessionStorage,
                                paths: ['user_search_history']
                     },
                     {
                            key: 'index_Carousel_allinfo',
                            storage: sessionStorage,
                            paths: ['index_Carousel_allinfo']
                    },
                    {
                            key: 'text_data',
                            storage: sessionStorage,
                            paths: ['text_data']
                    }, {
                            key: 'current_reader_article',
                            storage:sessionStorage,
                            paths: ['current_reader_article']
                    }, {
                            key: 'curent_article_expand_info',
                            storage: sessionStorage,
                            paths: ['curent_article_expand_info']
                    },
                    {
                            key: 'search_text_data',
                            storage: sessionStorage,
                            paths: ['search_text_data']
                    }, {
                            key: 'searchQuery',
                            storage: sessionStorage,
                            paths: ['searchQuery']
                    }, {
                            key: 'my_text_info',
                            storage: sessionStorage,
                            paths: ['my_text_info']
                    }, {
                            key: 'my_text_info_all',
                            storage: sessionStorage,
                            paths: ['my_text_info_all']
                        }, {
                            key: 'mycomments',
                            storage: sessionStorage,
                            paths:['mycomments']
                        }
                ]
        }         
    }
)